package jimo.SSMStudent.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jimo.SSMStudent.mapper.GradeMapper;
import jimo.SSMStudent.pojo.Grade;
import jimo.SSMStudent.pojo.GradeExample;
import jimo.SSMStudent.pojo.Student;
import jimo.SSMStudent.pojo.StudentExample;
import jimo.SSMStudent.service.impl.GradeServiceImpl;
import jimo.SSMStudent.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/***
 * 关于Student的控件
 */
@Controller
@RequestMapping("stu")
public class StudentController {
    @Autowired
    private GradeServiceImpl gradeService;

    @Autowired
    private StudentServiceImpl studentService;

    @ResponseBody
    @RequestMapping(value = "servlets",method = RequestMethod.GET)
    public ModelAndView selectStudents(String index,Student student){
        ModelAndView mv = new ModelAndView();
        //1.获取参数
        //1.1模糊查找  servlets?stuname
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        String name = student.getStuname();//查询并去除前后空格
        String stuno = student.getStuno();//查询并去除前后空格
        Integer sex = student.getSex();
        sex = sex==null? -1:sex;//防止空异常
        if (name!=null&&!name.trim().equals("")){
            criteria.andStunameLike("%"+name.trim()+"%");
        }
        if (stuno!=null && !stuno.trim().equals("")){
            criteria.andStunoEqualTo(stuno.trim());
        }
        if (sex!=-1){
            criteria.andSexEqualTo(sex);
        }
        //1.2分页数据 linit 开始位置，显示条数
         index = index==null? "0":index;//如果为空则默认为第一页
        //2.调取service
        //2.1获取学生信息
        PageHelper.startPage(Integer.parseInt(index.trim()),5);
        List<Student> students =  studentService.selects(studentExample);
        PageInfo<Student> info = new PageInfo<>(students);//获取数据信息
        //3.存入数据跳转页面
        //3.1存入搜索记录
        mv.addObject("selectStu",student);
        //3.2存入当前页码数
        mv.addObject("info",info);
        //3.3存入学生信息
        mv.addObject("students",students);
       mv.setViewName("forward:/Educational/student/list.jsp");
       System.out.println("ModelAndView---->>>>\n"+"page:"+index+"\nstudents:"+students+"\nPageInfo:"+info);
    return mv;
    }
    @RequestMapping(value = "getGradeList",method = RequestMethod.GET)
    public ModelAndView getGid(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forward:/Educational/student/add.jsp");
        List<Grade> grades = gradeService.gradeAll();//查询全部年级信息
        modelAndView.addObject("grades",grades);
       return modelAndView;
    }
    @RequestMapping(value = "updatastu",method = RequestMethod.POST)
    public void updata(Student student, HttpServletResponse resp){
        //1.获取数据
        System.out.println("addStudents------>>>"+student);
        //2.调取service，更新数据
        int zt = studentService.update(student);
        //3.跳转页面，反馈修改结果
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (zt>0){
            writer.println("<script>alert('修改成功！');location.href='/stu/servlets';</script>");
        }else {
            writer.println("<script>alert('修改失败！');location.href='/stu/servlets?"+student.getStuid()+"';</script>");
        }
    }
    @RequestMapping(value = "findbyid",method = RequestMethod.GET)
    public void Byid(HttpServletRequest req, HttpServletResponse resp) {
        //1.获取id值
        int id = Integer.parseInt(req.getParameter("sid"));
        //2.调取service。通过id进行查询
        //2.1获取student信息
        Student student = studentService.selectById(id);
        req.setAttribute("student", student);
        //2.2获取年级信息
        List<Grade> grades = gradeService.gradeAll();
        req.setAttribute("grades", grades);
        //3.跳转页面
        try {
            req.getRequestDispatcher("/Educational/student/edit.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "finddelid",method = RequestMethod.GET)
    public void did(HttpServletRequest req, HttpServletResponse resp){
        //1.获取id值
        int id = Integer.parseInt(req.getParameter("sid"));
        //2.调取service。通过id进删除
        int zt = studentService.delById(id);
        //3.跳转页面刷新
       /* resp.setContentType("text/html;charset=utf-8");*/
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (zt>0){
            writer.println("<script>alert('删除成功！');location.href='/stu/servlets';</script>");
        }else {
            writer.println("<script>alert('删除失败！');location.href='/stu/servlets';</script>");
        }
    }
    @RequestMapping(value = "addstu",method = RequestMethod.POST)
    public void add(Student student, HttpServletResponse resp){
        System.out.println("addStudents------>>>"+student);
     /*   //1.获取数据乱码处理
        String stuno = student.getStuno();
        String stuname = student.getStuname();
        Integer gid = student.getGid();
        Integer sex = student.getSex();
        String email = student.getEmail();
        String phone = student.getPhone();
        String registered = student.getRegistered();
        String address = student.getAddress();
        String politics = student.getPolitics();
        String idnumber = student.getIdnumber();
        String introduction = student.getIntroduction();
        String profession = student.getProfession();
        //2.调取service，存入数据
        Student student = new Student();
        student.setStuno(stuno);
        student.setStuname(stuname);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdnumber(idnumber);
        student.setIntroduction(introduction);
        student.setProfession(profession);*/
        int zt = studentService.insert(student);
        //3.跳转页面刷新
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (zt>0){
            writer.println("<script>alert('新增成功！');location.href='/stu/servlets?service=getStudentList';</script>");
        }else {
            writer.println("<script>alert('新增失败！');location.href='/stu/servlets?service=getGradeList';</script>");
        }
    }

}
